<?php
/**
 * 下载管理
 * @author juxuan
 *
 */


class DownloadAdminAction extends Action{
	
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION['admin_id'])){
			header("Content-Type:text/html; charset=utf-8");
			$this->redirect('Index/login',null,3,"非法登录,3秒后为你跳转到登录页面..");
		}
	}
	
	public function index(){	//进入下载管理首页
		import("ORG.Util.Page");
		$DownLoadFiles = new Model("Downloadfiles");

		//本科生下载文件
		$count = $DownLoadFiles->where('type=1')->count();
	    $Page = new Page($count, 15);
	    $show = $Page->show();		
		$undergraduateFiles = $DownLoadFiles->where('type=1')->order('file_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();	//本科生文件
		$this->assign("undergraduateFiles",$undergraduateFiles);
		$this->assign('page',$show); 
		
		//研究生下载文件
		$count1 = $DownLoadFiles->where('type=0')->count();
    	$Page1 = new Page($count1, 15);
    	$show1 = $Page1->show();
    	
		$graduateFiles = $DownLoadFiles->where('type=0')->order('file_id desc')->limit($Page1->firstRow.','.$Page1->listRows)->select();	//研究生文件
		$this->assign("graduateFiles",$graduateFiles);
		$this->assign('page1',$show1); 
		
		$this->display();
	}
	
	
	
	/**
     +----------------------------------------------------------
     * 删除文件处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
	public function deleteFile(){
		
		$DownloadFileForm = D("Downloadfiles");
    	$file_id = (int)$_GET['file_id'];
    	
    	$this->assign("file_id",$file_id);
    	$this->display(); 
	}
	
	
	//删除确认提交
    public function deleteCommit(){
    	$DownloadFileForm = D("Downloadfiles");
    	$file_id = (int)$_POST['file_id'];
    	
    	$DownloadFileForm->find($file_id);
	
    	if($DownloadFileForm->delete()){   	
    		$this->ajaxReturn(0,'文件删除成功！',1);	
    	}else{
    		$this->ajaxReturn('文件删除失败！');
    	}
    }
    
    /**
     +----------------------------------------------------------
     * 添加文件处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    public function insertFile() {
    	
    	if(!empty($_FILES)){
    		//如果有文件上传 上传附件
			$this->_upload();
    	}
    }
    
    
	// 文件上传
    protected function _upload()
    {
        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
       // $upload->maxSize  = 3292200 ;
     
        //设置附件上传目录
        $upload->savePath =  'Public/downloadFiles/';
	 
	   //设置上传文件规则
	  //  $upload->saveRule = uniqid;
	 

        if(!$upload->upload()) {
            //捕获上传异常
            header("Content-Type:text/html; charset=utf-8");
    		exit($upload->getErrorMsg().'[<a href="javascript:history.back()">返回</a>]');
        }else {
            //取得成功上传的文件信息
            $uploadFile = $upload->getUploadFileInfo();
           
        }
        
        $DownloadFileForm = M("Downloadfiles");
        
        
        $showName = trim($_POST['showname']); 
        //保存当前数据对象
        if($showName != ''){
        	$file['showname'] = $_POST['showname'];
        }
        else{
        	$file['showname'] = $uploadFile[0]['savename'];
        }
      	$file['filename'] = $uploadFile[0]['savename'];
      	$file['type'] = $_POST['type'];
      	
      
        $list=$DownloadFileForm->add ($file);
        if($list!==false){
            $this->success ('上传文件成功！');
        }else{
            $this->error ('上传文件失败!');
        }
	}
	
}

?>